using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Infragistics.Windows.DataPresenter;

using ViewOnCore.Helper.WPF;
using ViewOnCore.HostContract.Media;

namespace UI.Control
{
    public partial class VideoThumbnail : System.Windows.Controls.UserControl
    {

        #region Variables

        public static readonly DependencyProperty MediaDataProperty =
            DependencyProperty.Register("MediaData",
                                        typeof(MediaData),
                                        typeof(VideoThumbnail),
                                        new PropertyMetadata(null, new PropertyChangedCallback(mediaData_changed)));

        public static readonly DependencyProperty DataRecordProperty =
            DependencyProperty.Register("DataRecord",
                                typeof(DataRecord),
                                typeof(VideoThumbnail),
                                new PropertyMetadata(null, new PropertyChangedCallback(dataRecord_changed)));

        public static readonly DependencyProperty IsSelectedProperty =
            DependencyProperty.Register("IsSelected",
                                        typeof(bool),
                                        typeof(VideoThumbnail),
                                        new PropertyMetadata(false, new PropertyChangedCallback(isSelected_changed)));

        //---- Hacks for edition
        private static List<VideoThumbnail> _selecteds = new List<VideoThumbnail>();
        private bool _isEditing = false;

        private XamDataPresenter _presenter;

        #endregion

        #region Constructor

        public VideoThumbnail()
        {
            InitializeComponent();

            txtTitle.MouseLeftButtonUp += new MouseButtonEventHandler(txtTitle_MouseLeftButtonUp);

            //recEdition.MouseLeftButtonDown += new MouseButtonEventHandler(recEdition_MouseLeftButtonDown);

            txtTitleEdit.KeyDown += new KeyEventHandler(txtTitleEdit_KeyDown);
            txtTitleEdit.LostFocus += new RoutedEventHandler(txtTitleEdit_LostFocus);
        }

        #endregion

        #region Properties

        public DataRecord DataRecord
        {
            get
            {
                return (DataRecord)GetValue(DataRecordProperty);
            }
            set
            {
                SetValue(DataRecordProperty, value);
            }
        }

        public MediaData MediaData
        {
            get
            {
                return (MediaData)GetValue(MediaDataProperty);
            }
            set
            {
                SetValue(MediaDataProperty, value);
            }
        }

        public bool IsSelected
        {
            get
            {
                return (bool)GetValue(IsSelectedProperty);
            }
            set
            {
                SetValue(IsSelectedProperty, value);
            }
        }

        #endregion

        #region Editing

        public void StartEditing()
        {
            _presenter.AllowDrop = false;

            _isEditing = true;

            txtTitleEdit.Text = txtTitle.Text;
            txtTitle.Visibility = Visibility.Collapsed;

            txtTitleEdit.Visibility = Visibility.Visible;
            txtTitleEdit.Focus();
            txtTitleEdit.SelectAll();
        }

        //void recEdition_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        //{
        //    if (_isEditing || e.ClickCount != 2)
        //        return;

        //    _editingVideoLibraryDialog = dialog;

        //    StartEditing();
        //    e.Handled = true;
        //}

        void txtTitle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (_isEditing || e.ClickCount != 2)
                return;

            StartEditing();
            e.Handled = true;
        }

        void txtTitleEdit_LostFocus(object sender, RoutedEventArgs e)
        {
            if (_isEditing)
                OnEndEdit(true);
        }

        void txtTitleEdit_KeyDown(object sender, KeyEventArgs e)
        {
            if (!_isEditing)
                return;

            if (e.Key == Key.Enter)
                OnEndEdit(true);

            if (e.Key == Key.Escape)
                OnEndEdit(false);
        }

        private void OnEndEdit(bool acceptChanges)
        {
            _isEditing = false;

            txtTitleEdit.Visibility = Visibility.Hidden;
            txtTitle.Visibility = Visibility.Visible;

            //---- End of the edition
            string newValue = txtTitleEdit.Text.Trim();

            if (acceptChanges && !_editingVideoLibraryDialog.OnEditModeEnding(newValue, DataRecord))
                acceptChanges = false;

            if (acceptChanges && newValue.Length > 0)
                txtTitle.Text = newValue;

            _presenter.AllowDrop = true;

            _editingVideoLibraryDialog = null;
        }

        #endregion

        #region Events

        static private void mediaData_changed(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue == null)
                return;

            if (!ViewOnCore.Visual.VisualPartHelper.IsCurrentMediaDeviceSupported("MediaDesktop"))
                return;

            VideoThumbnail thumbnail = sender as VideoThumbnail;
            MediaData mediaData = args.NewValue as MediaData;

            mediaData.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(thumbnail.mediaData_PropertyChanged);

            thumbnail.UpdateContent();
        }

        static private void dataRecord_changed(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue == null)
                return;

            if (!ViewOnCore.Visual.VisualPartHelper.IsCurrentMediaDeviceSupported("MediaDesktop"))
                return;

            VideoThumbnail thumbnail = sender as VideoThumbnail;
            DataRecord dataRecord = args.NewValue as DataRecord;

            thumbnail._presenter = (XamDataPresenter)dataRecord.DataPresenter;
        }

        static private void isSelected_changed(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (!ViewOnCore.Visual.VisualPartHelper.IsCurrentMediaDeviceSupported("MediaDesktop"))
                return;

            VideoThumbnail thumbnail = sender as VideoThumbnail;
            if (thumbnail.IsSelected)
            {
                thumbnail.borSelection.Visibility = Visibility.Visible;
                lock (_selecteds)
                    _selecteds.Add(thumbnail);
            }
            else
            {
                thumbnail.borSelection.Visibility = Visibility.Collapsed;
                lock (_selecteds)
                    _selecteds.Remove(thumbnail);
            }
        }

        void mediaData_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (!ViewOnCore.Visual.VisualPartHelper.IsCurrentMediaDeviceSupported("MediaDesktop"))
                return;

            // Refresh the image
            Dispatcher.Invoke(DispatcherPriority.Normal,
                (DelegateInvoker)delegate { UpdateContent(); });
        }

        #endregion

        #region UpdateContent

        private void UpdateContent()
        {
            if (MediaData == null)
                return;

            txtTitle.Text = MediaData.Title;
            imgThumbail.Source = MediaData.IconBig;

            if (MediaData is MediaFolder)
            {
                tbRating1.Visibility = Visibility.Collapsed;
                tbRating2.Visibility = Visibility.Collapsed;
                tbRating3.Visibility = Visibility.Collapsed;
                tbRating4.Visibility = Visibility.Collapsed;
                tbRating5.Visibility = Visibility.Collapsed;
            }
            else
            {
                tbRating1.IsChecked = ((Video)MediaData).IsRating1;
                tbRating2.IsChecked = ((Video)MediaData).IsRating2;
                tbRating3.IsChecked = ((Video)MediaData).IsRating3;
                tbRating4.IsChecked = ((Video)MediaData).IsRating4;
                tbRating5.IsChecked = ((Video)MediaData).IsRating5;
            }
        }

        #endregion

        #region RequestEdit : HACK

        private VideoLibraryDialog _editingVideoLibraryDialog;

        static public void RequestEdit(XamDataPresenter presenter, VideoLibraryDialog dialog)
        {
            foreach (VideoThumbnail thumbnail in _selecteds)
                if (thumbnail.DataRecord.DataPresenter == presenter)
                {
                    thumbnail._editingVideoLibraryDialog = dialog;
                    thumbnail.StartEditing();
                    return;
                }
        }

        #endregion

    }
}